package com.cet.spring_batch_study.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

/**
 * @author NSOC
 */
@Configuration
@RequiredArgsConstructor
public class DatasourceConfig {

    private final Environment env;

    @Bean
    public DataSource batchDatasource() {
        // SpringBoot默认使用Hikari数据库连接池
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(env.getProperty("spring.batchDataSource.url"));
        config.setUsername(env.getProperty("spring.batchDataSource.username"));
        config.setPassword(env.getProperty("spring.batchDataSource.password"));
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        config.setMaximumPoolSize(50);
        config.setPoolName("batch_thread_pool");
        return new HikariDataSource(config);
    }
}
